package com.aiyige.page.globalsearch.adapter;

import android.text.Html;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.city.model.City;
import com.aiyige.page.globalsearch.model.SearchGroup;
import com.aiyige.utils.DummyDataUtil;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/12/11.
 */

public class MajorCourseAdapter extends BaseQuickAdapter<SearchGroup.Sample, MajorCourseAdapter.ViewHolder> {
    public MajorCourseAdapter() {
        super(R.layout.global_search_major_course_item, new LinkedList<SearchGroup.Sample>());
    }

    @Override
    protected void convert(ViewHolder helper, SearchGroup.Sample item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.locationIv)
        ImageView locationIv;
        @BindView(R.id.locationTv)
        TextView locationTv;
        @BindView(R.id.durationTv)
        TextView durationTv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.buyNumTv)
        TextView buyNumTv;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            GlideUtil.with(itemView.getContext())
                    .loadCover(data.getCover())
                    .into(coverIv);
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(data.getOwnerAvatar())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);
            priceTv.setText(StringUtils.priceFormat(data.getPrice()));
            buyNumTv.setText(String.format(StringUtils.getString(R.string.buy_num), StringUtils.formatNum((int) data.getSellCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
            durationTv.setText(String.format(StringUtils.getString(R.string.course_num_label), String.valueOf(data.getDurationTotal())));
            try {
                City city = DummyDataUtil.getCityById(data.getCity());
                locationTv.setText(city.getCity());
            } catch (Exception e) {
                locationTv.setText("");
            }
        }
    }
}
